package com.crack.nga2.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;

import com.crack.nga2.R;
import com.crack.nga2.custom.HeadLayoutImpl;
import com.crack.nga2.custom.PullListView;
import com.crack.nga2.service.InternetStateService;
import com.crack.nga2.span.BitmapCache;
import com.crack.nga2.span.ImageSpanWrapper;
import com.crack.nga2.span.SpanUtil;
import com.crack.nga2.utils.GlobalConst;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logo);
    }

    /**
     * 初始化全局变量
     */
    private void initGlobalConst() {
        SpanUtil.collapseDrawable = getResources().getDrawable(R.drawable.collapsed_no);
        SpanUtil.collapseDrawable.setBounds(0,0,40,40);
        SpanUtil.listDrawable = getResources().getDrawable(R.drawable.diandian_on);
        SpanUtil.listDrawable.setBounds(0,0,20,20);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        String[] options = getResources().getStringArray(R.array.settings);
        for (int i = 1; i <= 7; i++) {
            String item = options[i];
            boolean b = sp.getBoolean(item, false);
            GlobalConst.settings.put(item, b);
        }
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        GlobalConst.SCREEN_WIDTH = outMetrics.widthPixels;
        GlobalConst.SCREEN_HEIGHT = outMetrics.heightPixels;
        PullListView.MAX_HEIGHT = GlobalConst.SCREEN_HEIGHT / 6;
        HeadLayoutImpl.WIDTH = GlobalConst.SCREEN_WIDTH;
        File file = new File(getFilesDir(), "cookie.cache");
        Log.i("tag", file + ":");
        if (file.exists()) {
            try {
                FileReader reader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(reader);
                GlobalConst.Cookie = bufferedReader.readLine();
                bufferedReader.close();
                GlobalConst.isLogin = true;
                //获取uid
                Pattern p = Pattern.compile("(?<=ngaPassportUid=)(\\d+?)(?=;)");
                Matcher m = p.matcher(GlobalConst.Cookie);
                if (m.find()) {
                    GlobalConst.UID = Integer.parseInt(m.group());
                }
                //获取用户名
                p = Pattern.compile("(?<=ngaPassportUrlencodedUname=)(.+?)(?=;)");
                m = p.matcher(GlobalConst.Cookie);
                if (m.find()) {
                    GlobalConst.username = URLDecoder.decode(URLDecoder.decode(m.group(), "GBK"), "GBK");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        ImageSpanWrapper.defaultDrawable = getResources().getDrawable(R.drawable.loading_img);
        int width = ImageSpanWrapper.defaultDrawable.getIntrinsicWidth();
        int height = ImageSpanWrapper.defaultDrawable.getIntrinsicHeight();
        float f = width * 1.0f / GlobalConst.SCREEN_WIDTH;
        height = (int) (height * 1.0 / f);
        width = GlobalConst.SCREEN_WIDTH;
        Log.i("log", width + "," + height);
        ImageSpanWrapper.defaultDrawable.setBounds(0, 0, width, height);

    }

    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(LogoActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 1000);
        startService(new Intent(this, InternetStateService.class));
        initGlobalConst();
       /* //初始化图片加载配置
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .threadPoolSize(2)
                .memoryCacheSize(15 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                .defaultDisplayImageOptions(options)
                .build();

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);*/
    }
}
